package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;

import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 新增地址
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
       log.info("地址为{}",addressBook);
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>(); //是updateWrapper
        queryWrapper.eq(AddressBook :: getUserId,BaseContext.getCurrentId());
        //先把他设置默认为0
        queryWrapper.set(AddressBook :: getIsDefault,0);
        addressBookService.update(queryWrapper);
        //再根据操作设置为1 1是默认地址
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }
    /**
     * 根据id来查询地址
     */
    @GetMapping("/{id}")
    public R get(@PathVariable Long id){
        AddressBook byId = addressBookService.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("查询不到该地址！");
    }


    /**
     * 查询默认地址
     */

    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        //request.getCurrentId()
        queryWrapper.eq(AddressBook ::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook ::getIsDefault,1);
        AddressBook one = addressBookService.getOne(queryWrapper);
        if ( null == one) {
            return R.error("找不到该对象");
        }
        return R.success(one);
    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        //null ！= ??? 因为判断是左往右判断的，所以null肯定为真这就不会报空指针了！
        queryWrapper.eq(null!=addressBook.getUserId(),AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }

}
